package com.xxgc.springboot.controller.exception.aop;

import com.xxgc.springboot.controller.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: springboot
 * @description: 添加异常接口
 * @author: Mr.abel(ShiJiaYi)
 * @create: 2022-09-22 10:26
 **/
@Slf4j
@Aspect
@Component
public class AddExceptionPath {

    @AfterThrowing(value = "execution (* com.xxgc.springboot.controller.*.*(..))",throwing = "ex")
    public void beforeAdvice(JoinPoint joinPoint, BaseException ex) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //log.error("接口URL        "+request.getRequestURI().toString());
        //log.error("方法 Http Method      "+request.getMethod());
        //log.error("全路径 IP        "+request.getRemoteAddr());
        Signature signature = joinPoint.getSignature();
        //log.error("方法名        "+signature.getDeclaringTypeName()+"."+signature.getName());
        //给返回异常添加请求路径
        ex.setPath(request.getRequestURI().toString());
        //给返回路径添加错误方法
        ex.setMethodName(signature.getDeclaringTypeName()+"."+signature.getName());
        /**
         * 接下来的时间
         * 数据库创表，表字段自己定义
         * 把错误信息和能搜集到的所有信息保存在数据库的异常表中
         */
    }



}
